package fastjson.code;

import com.alibaba.fastjson.JSONArray;

import java.util.*;

/**
 * Description: JSONArray示例demo
 *
 * @author code-worker
 * @date 2021/08/19 21:58
 */
public class JsonArrayDemo {
    private String name;
    private String desc;
    private JsonArrayDemo(){}
    private JsonArrayDemo(String name, String desc){
        this.name = name;
        this.desc = desc;
    }

    @Override
    public String toString() {
        return "JsonArrayDemo{" +
                "name='" + name + '\'' +
                ", desc='" + desc + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public static void main(String[] args) {
        JSONArray jsonArray = new JSONArray(3);
        JsonArrayDemo jsonArrayDemo = new JsonArrayDemo("Test", "Test");
        System.out.println("1.insert data: "+jsonArray.fluentAdd(jsonArrayDemo)
                .fluentAdd(new JsonArrayDemo("name", "desc"))
                .fluentAdd(new JsonArrayDemo()));
        System.out.println("2.toArray method: "+Arrays.toString(jsonArray.toArray(new Object[0])));
        for (Object o : jsonArray) {
            System.out.println("3.traverse: "+o);
        }
        List<JsonArrayDemo> jsonList = new ArrayList<>();
        jsonList.add(jsonArrayDemo);
        System.out.println("4.retain: "+jsonArray.fluentRetainAll(jsonList));
        System.out.println("5.index of: "+jsonArray.indexOf(jsonArrayDemo));
    }

}
